package com.abu.crms.service.impl;


import cn.hutool.core.util.StrUtil;
import com.abu.crms.config.exception.CRException;
import com.abu.crms.config.mvc.LoginInterceptor;
import com.abu.crms.mapper.ApplyReportMapper;
import com.abu.crms.mapper.ManagerMapper;
import com.abu.crms.pojo.ApplyReport;
import com.abu.crms.pojo.Manager;
import com.abu.crms.service.ApplyReportService;
import com.abu.crms.utils.PageUtils;
import com.abu.crms.vo.ApplyReportVO;
import com.abu.crms.vo.QueryApplyReportVo;
import com.abu.crms.vo.QueryConditionVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 申请记录表 服务实现类
 * </p>
 *
 * @author 阿布
 * @since 2021-02-03
 */
@Service
public class ApplyReportServiceImpl extends ServiceImpl<ApplyReportMapper, ApplyReport> implements ApplyReportService {

    @Autowired
    private ApplyReportMapper applyReportMapper;

    @Autowired
    private ManagerMapper managerMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    private static final String CR_QUEUE = "CLASSROOM_QUEUE";

    @Override
    public HashMap<String, Object> queryApplyByPage(QueryApplyReportVo queryApplyReportVo) {
//        构建分页条件
        Long limit = queryApplyReportVo.getLimit();
        Long page = queryApplyReportVo.getPage();
//       不同数字代表不同column
        Integer type = queryApplyReportVo.getType();
        String key = queryApplyReportVo.getKey();
        String column = null;
        if (type != null) {
            switch (type) {
                case 1:
                    column = "applicant_email";
                    break;
                case 2:
                    column = "classroom_name";
                    break;
                case 3:
                    column = "applicant";
                    break;
                default:
                    break;
            }
        }
        LocalDateTime startTime = queryApplyReportVo.getStartTime();
        LocalDateTime endTime = queryApplyReportVo.getEndTime();
//        获取当前用户登录的id
        String managerId = LoginInterceptor.getManagerId();
//        分页条件
        Page<ApplyReport> reportPage = new Page<>(page, limit);
        QueryWrapper<ApplyReport> reportQueryWrapper = new QueryWrapper<>();
        reportQueryWrapper
                .like(column != null, column, key)
                .ge(startTime != null, "begin_time", startTime)
                .le(endTime != null, "end_time", endTime)
                .eq(!StrUtil.equalsAny(managerId, "0", "1"), "reviewer_id", managerId);
//        执行查询
        Page<ApplyReport> applyReportPage = this.applyReportMapper.selectPage(reportPage, reportQueryWrapper);
//        获取总条数
        long total = applyReportPage.getTotal();
//       封装结果集
        List<ApplyReport> applyReportList = applyReportPage.getRecords();
        List<ApplyReportVO> applyReportVOList = applyReportList.stream().map(applyReport -> {
            ApplyReportVO applyReportVO = new ApplyReportVO();
            Manager manager = this.managerMapper.selectById(applyReport.getReviewerId());
            String mgName = manager.getMgName();
            applyReportVO.setReviewer(mgName);
            BeanUtils.copyProperties(applyReport, applyReportVO);
            return applyReportVO;
        }).collect(Collectors.toList());
        //        获取结果集
        HashMap<String, Object> mapResult = new HashMap<>(2);
        mapResult.put("applyReportVOList", applyReportVOList);
        mapResult.put("total", total);
        return mapResult;
    }

    @Override
    public boolean updateState(Long applyId, Integer status) {
//        获取管理员登录名
        String userId = LoginInterceptor.getManagerId();
        Manager manager = this.managerMapper.selectById(userId);

        ApplyReportVO applyReportVO = new ApplyReportVO();
        applyReportVO.setReviewer(manager.getMgName());
        applyReportVO
                .setApplyId(applyId)
                .setApplyStatus(status)
                .setReviewerTime(LocalDateTime.now());
        int count = this.applyReportMapper.updateById(applyReportVO);
//        获取更新后的数据
        this.rabbitTemplate.convertAndSend(CR_QUEUE, applyId);
        return count == 1;
    }

    @Override
    public Integer queryNonInspectApplyReport(Long reviewerId, Integer status) {
        if (reviewerId == 0 || reviewerId == 1) {
            return this.applyReportMapper.selectCount(new QueryWrapper<ApplyReport>().eq("apply_status", status));
        }
        return this.applyReportMapper.queryNonInspectApplyReport(reviewerId, status);
    }
}
